/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.handlers.commands.call.animation;

import com.hercules.android.receiver.model.commands.XRECallCommand;
import com.hercules.android.receiver.model.commands.XRECommand;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Helper class for this component.
 * <p>
 * <b>Thread safety:</b> This class is immutable and so thread safe.
 *
 * @author 7realm
 * @version 1.0
 */
final class Helper {
    /**
     * This private constructor prevents to create a new instance.
     */
    private Helper() {
        // empty
    }

    /**
     * Checks whether the given {@link Object} is null.
     *
     * @param arg the argument to check
     * @param name the name of the argument to check
     * @throws IllegalArgumentException if the given object is null
     */
    static void checkNotNull(Object arg, String name) {
        if (arg == null) {
            throw new IllegalArgumentException("Argument '" + name + "' value should not be null.");
        }
    }

    /**
     * Checks passed command and casts it to {@link XRECallCommand}.
     *
     * @param command the command to check
     * @return the casted to {@link XRECallCommand} command
     * @throws IllegalArgumentException if command is incorrect
     */
    static XRECallCommand checkCommand(XRECommand command) {
        checkNotNull(command, "command");
        if (!(command instanceof XRECallCommand)) {
            throw new IllegalArgumentException("Passed command " + command + " is not XRECallCommand.");
        }

        XRECallCommand callCommand = (XRECallCommand) command;
        if (!"animate".equals(callCommand.getMethod())) {
            throw new IllegalArgumentException("Passed command " + command + " must have 'animate' method.");
        }
        return callCommand;
    }

    /**
     * Logs error.
     *
     * @param logger the logger
     * @param <T> type of occurred exception
     * @param className the name of class, where error occurred
     * @param methodName the name of the method, where error occurred
     * @param e the occurred exception
     * @return passed in exception
     */
    static <T extends Exception> T logError(Logger logger, String className, String methodName, T e) {
        logger.logp(Level.SEVERE, className, methodName, "Exception occurred in method. Details " + e.getMessage(), e);
        return e;
    }
}
